export default function (res) {
  console.log(res);
  const disposition = res.header['Content-Disposition'];
  let fileName = disposition.split(';')[1].split('filename=')[1];
  fileName = decodeURIComponent(fileName);
  console.log(fileName);
  const txtContent = res.data; // 要导出的文本内容
  console.log(txtContent);
  
  //截取文件后缀
  let suffix = getFileExtension(fileName);
  console.log(suffix);
  //判断文件为txt还是word
  if(suffix === 'txt' || suffix === 'docx'){
    wx.getFileSystemManager().access({
      path: wx.env.USER_DATA_PATH + '/export',
      success: function () {
        saveFile();
      },
      fail: function () {
        wx.getFileSystemManager().mkdir({
          dirPath: wx.env.USER_DATA_PATH + '/export',
          recursive: true,
          success: function () {
            saveFile();
          },
          fail: function (err) {
            console.error('创建目录失败', err);
            wx.showToast({
              title: '导出失败',
              icon: 'none',
            });
          },
        });
      },
    });
  }
  function saveFile() {
    const filePath = wx.env.USER_DATA_PATH + '/export/' + fileName;
    wx.getFileSystemManager().writeFile({
      filePath: filePath,
      data: txtContent,
      encoding: 'utf8',
      success: function (res) {
        console.log(res);
        wx.showToast({
          title: '导出成功',
          icon: 'success',
        })
      },
      fail: function (err) {
        console.error('保存文件失败', err);
        wx.showToast({
          title: '导出失败',
          icon: 'none',
        });
      },
    })
  }

  function getFileExtension(filename) {
    const index = filename.lastIndexOf('.');
    if (index !== -1 && index < filename.length - 1) {
      return filename.substr(index + 1).toLowerCase();
    }
    return '';
  }
}